home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1993 / Internet Info CD-ROM (Walnut Creek) (1993).iso / networking / terms / tip / hunt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-13  |  3.0 KB  |  115 lines

  1. /*
  2.  * Copyright (c) 1983 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)hunt.c    5.4 (Berkeley) 9/2/88";
  20. #endif /* not lint */
  21.  
  22. #include "tip.h"
  23.  
  24. extern char *getremote();
  25. extern char *rindex();
  26.  
  27. static    jmp_buf deadline;
  28. static    int deadfl;
  29.  
  30. char uucplock[BUFSIZ];
  31.  
  32. dead()
  33. {
  34.  
  35.     deadfl = 1;
  36.     longjmp(deadline, 1);
  37. }
  38.  
  39. hunt(name)
  40.     char *name;
  41. {
  42.     register char *cp, *cf, *ct;
  43.     sigfunc_t (*f)();
  44.  
  45.     f = signal(SIGALRM, dead);
  46.     while (cp = getremote(name)) {
  47.         /*
  48.          * The name of the lock is pretty important. We don't
  49.          * want to introduce the following security problems:
  50.          * 1. Namecollision in lock file between different devices
  51.          * 2. uucplock is used to decide whether or not to call
  52.          *    acucntrl. Thus, non-/dev devices should have a different
  53.          *    lock name than devices in /dev.
  54.          */
  55.         if (*cp != '/')    /* device name must be absolute path */
  56.             continue;
  57.         while (*cp == '/') cp++;
  58.         cp--;
  59.         if (strncmp("/dev/", cp, 5) == 0) {
  60.             cf = cp + 5;
  61.             while (*cf == '/') cf++;
  62.         } else
  63.             cf = cp;
  64.         ct = uucplock;
  65.         while (*cf) {
  66.             if (*cf == '/') {
  67.                 if (ct == uucplock
  68.                     || (ct > uucplock && *(ct-1) != '_'))
  69.                     *ct++ = '_';
  70.             } else
  71.                 *ct++ = *cf;
  72.             cf++;
  73.         }
  74.         *ct = '\0';
  75.         /*
  76.          * At this point we are guranteed that any devices outside
  77.          * /dev will have a lock name starting with underscore (_),
  78.          * and that any slashes (/) in the pathname have been changed
  79.          * to underscores, and that multiple slashes are only made
  80.          * into a single underscore. Thus, any way of expressing
  81.          * a pathname will lead to a unique lock file name.
  82.          */
  83.         deadfl = 0;
  84.         if (uu_lock(uucplock) < 0)
  85.             continue;
  86.         /*
  87.          * Straight through call units, such as the BIZCOMP,
  88.          * VADIC and the DF, must indicate they're hardwired in
  89.          *  order to get an open file descriptor placed in FD.
  90.          * Otherwise, as for a DN-11, the open will have to
  91.          *  be done in the "open" routine.
  92.          */
  93.         if (!HW)
  94.             break;
  95.         if (setjmp(deadline) == 0) {
  96.             alarm(10);
  97.             FD = open(cp, O_RDWR);
  98.         }
  99.         alarm(0);
  100.         if (FD < 0) {
  101.             perror(cp);
  102.             deadfl = 1;
  103.         }
  104.         if (!deadfl) {
  105.             ioctl(FD, TIOCEXCL, 0);
  106.             ioctl(FD, TIOCHPCL, 0);
  107.             signal(SIGALRM, SIG_DFL);
  108.             return ((int)cp);
  109.         }
  110.         (void)uu_unlock(uucplock);
  111.     }
  112.     signal(SIGALRM, f);
  113.     return (deadfl ? -1 : (int)cp);
  114. }
  115.